package com.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/12/22 19:38
 **/

@WebServlet(name = "CountServlet", value = "/CountServlet")
public class CountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int totalCount=1;//默认访问量为1

        //获取网站访问量

        Object count = request.getServletContext().getAttribute("count");
        //判断count是否为null，不为null表示曾经访问过，直接将count赋值给totalCount
        if(count!=null) {
            totalCount=(int)count;
        }

        //访问次数累加
        request.getServletContext().setAttribute("count",totalCount+1);
        //跳转到接收页面
        response.sendRedirect(request.getHeader("Referer"));
    }
}
